package com.xlabs.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;

/**
 * The main entry point for Geeky.
 */
public class Geeky implements EntryPoint {

    /**
     * The main panel.
     */
    private DockPanel mainPanel;

    /**
     * youtube search widget.
     */
    private YoutubeAdapter youtubeAdapterWidget = new YoutubeAdapter("Player1");

    /**
     * The comment box.
     */
    private TextArea commentBox = new TextArea();

    /**
     * The comment panel.
     */
    private VerticalPanel commentPanel = new VerticalPanel();

    /**
     * Current video id.
     */
    private String currentVideoId;

    /**
     * Comment view panel.
     */
    private VerticalPanel questionView = new VerticalPanel();

    /**
     * Scrollbar for comments.
     */
    private Questions questions;

    /**
     * Schollbar for coversations.
     */
    private Conversation conversation = new Conversation();

    /**
     * header;
     */
    private Header header;

    /**
     * On Module Load.
     */
    public void onModuleLoad() {

        this.initializeMainPanel();

        this.initializeHeader();

        this.initializeVideoPanel();

        this.initializeCommentingView();

        this.initializeQuestionPanel();

        this.loadQuestions();
        
        // Associate the Main panel with the HTML host page.
        RootPanel.get("geekyPage").add(mainPanel);
    }

    /**
     * Initializes the header.
     */
    private void initializeHeader() {
        this.header = new Header();
        
        this.mainPanel.add(this.header, DockPanel.NORTH);
    }

    /**
     * Initializes comment view.
     */
    private void initializeCommentingView() {
//        HorizontalPanel commentingPanel = new HorizontalPanel();

//        commentingPanel.add(this.questionView);
//        commentingPanel.add(this.conversation);

        mainPanel.add(this.questionView, DockPanel.WEST);
        mainPanel.add(this.conversation, DockPanel.EAST);

        questions = new Questions(this.youtubeAdapterWidget, this.conversation);
        questions.setSize("600", "300");
        questions.addStyleName("ScrollPanel");

        this.questionView.add(questions);
    }

    /**
     * Initializes video panel.
     */
    private void initializeVideoPanel() {
        VerticalPanel videoPanel = new VerticalPanel();

        HorizontalPanel controlButtonsLayout = new HorizontalPanel();

        VerticalPanel displayAndControls = new VerticalPanel();
        displayAndControls.add(this.youtubeAdapterWidget);
        displayAndControls.add(controlButtonsLayout);
        videoPanel.add(displayAndControls);

        this.initializeControlButtons(controlButtonsLayout);

        videoPanel.add(this.commentPanel);
        
        mainPanel.add(videoPanel, DockPanel.SOUTH);
    }

    /**
     * Initializes comment panel.
     */
    private void initializeQuestionPanel() {
        this.commentPanel.setVisible(false);

        this.commentBox.setPixelSize(600, 80);
        this.commentPanel.add(this.commentBox);

        Button submitQuestion = new Button("Ask Question", new ClickListener() {
            public void onClick(Widget widget) {

                CommentingServiceAsync myService = (CommentingServiceAsync) GWT.create(CommentingService.class);

                AsyncCallback postCallback = new AsyncCallback() {
                    public void onFailure(Throwable throwable) {
                        commentBox.setText("Post Comment Failed!" + throwable + "\n" + commentBox.getText());
                    }

                    public void onSuccess(Object o) {
                        questions.displayQuestions((CommentDTO[]) o);
                        commentBox.setText("");
                    }
                };

                double time = Math.max(youtubeAdapterWidget.getTime(), 0) ;
                myService.postQuestion(commentBox.getText(), time, currentVideoId, postCallback);
            }
        });

        this.commentPanel.add(submitQuestion);
    }

    /**
     * Loads all comments.
     */
    private void loadQuestions() {
        CommentingServiceAsync service = (CommentingServiceAsync) GWT.create(CommentingService.class);

        AsyncCallback getCallBack = new AsyncCallback() {
            public void onFailure(Throwable throwable) {
                Geeky.this.commentBox.setText("Failed to load comments " + throwable + "\n" + commentBox.getText());
            }

            public void onSuccess(Object o) {
                Geeky.this.questions.displayQuestions((CommentDTO[]) o);
                Geeky.this.commentBox.setText("");
            }
        };

        service.getQuestions(currentVideoId, getCallBack);
    }

    /**
     * Initializes main panel.
     */
    private void initializeMainPanel() {
        this.mainPanel = new DockPanel();
        mainPanel.setHorizontalAlignment(DockPanel.ALIGN_LEFT);
    }

    /**
     * Initializes control buttons
     * @param controlButtonsLayout buttons layout.
     */
    private void initializeControlButtons(HorizontalPanel controlButtonsLayout) {
        Button startButton = new Button("Start", new ClickListener() {
            public void onClick(Widget widget) {
                currentVideoId = "hNmhihKIINM";
                youtubeAdapterWidget.loadVideoById(currentVideoId, 0);
            }
        });

        Button pauseButton = new Button("Pause", new ClickListener() {
            public void onClick(Widget widget) {
                youtubeAdapterWidget.pauseVideo();
                commentPanel.setVisible(true);
                commentBox.setText("Question at time : " + youtubeAdapterWidget.getTime());
                commentBox.addClickListener(new ClickListener() {
                    public void onClick(Widget widget) {
                        commentBox.setText("");
                        commentBox.removeClickListener(this);
                        commentBox.setFocus(true);
                    }
                });
                commentBox.setFocus(true);
            }
        });

        Button playButton = new Button("Play", new ClickListener() {
            public void onClick(Widget widget) {
                Geeky.this.commentPanel.setVisible(false);
                youtubeAdapterWidget.playVideo();
            }
        });

        controlButtonsLayout.add(startButton);
        controlButtonsLayout.add(pauseButton);
        controlButtonsLayout.add(playButton);
    }
}
